define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {

	var Controller = {
        index: function () {
        	Controller.api.bindevent();
            $("#c-businessArea1").trigger("fa.event.businessAreaupdated",'edit');
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');
            $("#c-bankAreaIds1").trigger("fa.event.bankAreaIdsupdated",'edit');
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //四级地区联动参数传递
                $("#c-businessArea2").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea1").val()}};
                });
                $("#c-businessArea3").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea2").val()}};
                });
                $("#c-businessArea4").data("params", function (obj) {
                    return {custom: {pid: $("#c-businessArea3").val()}};
                });

                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: $("#c-category1").val()}};
                });
                $("#c-category3").data("params", function (obj) {
                    return {custom: {pid: $("#c-category2").val()}};
                });
                $("#c-category4").data("params", function (obj) {
                    return {custom: {pid: $("#c-category3").val()}};
                });

                $("#c-bankAreaIds2").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds1").val()}};
                });
                $("#c-bankAreaIds3").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds2").val()}};
                });
                $("#c-bankAreaIds4").data("params", function (obj) {
                    return {custom: {pid: $("#c-bankAreaIds3").val()}};
                });
                //一级地区选定，清除二、三、四级选项
                $(document).on("change", "#c-businessArea1", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea2').selectPageClear();
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($('#c-businessArea1').val());
                    });
                });
                $(document).on("change", "#c-category1", function () {
                    require(['selectpage'], function () {
                        $('#c-category2').selectPageClear();
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaId").val($('#c-category1').val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds1", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds2').selectPageClear();
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($('#c-bankAreaIds1').val());

                    });
                });
                //二级地区选定，清除三、四级选项
                $(document).on("change", "#c-businessArea2", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea3').selectPageClear();
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val());
                    });
                });
                $(document).on("change", "#c-category2", function () {
                    require(['selectpage'], function () {
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-areaId").val($("#c-category2").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds2", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds3').selectPageClear();
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val());

                    });
                });
                //三级地区选定，清除四级选项
                $(document).on("change", "#c-businessArea3", function () {
                    require(['selectpage'], function () {
                        $('#c-businessArea4').selectPageClear();
                        $("#c-businessAreaPath").val( $("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val());
                    });
                });
                $(document).on("change", "#c-category3", function () {
                    require(['selectpage'], function () {
                        $('#c-category4').selectPageClear();
                        $("#c-areaId").val($("#c-category3").val());
                    });
                });
                $(document).on("change", "#c-bankAreaIds3", function () {
                    require(['selectpage'], function () {
                        $('#c-bankAreaIds4').selectPageClear();
                        $("#c-bankAreaIds").val($("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val());
                    });
                });
                //拼接四级地区选项值，用于保存
                $(document).on("change", "#c-category4", function () {
                    $("#c-businessAreaPath").val($("#c-businessArea1").val()+","+$("#c-businessArea2").val()+","+$("#c-businessArea3").val()+","+$("#c-businessArea4").val());
                });
                $(document).on("change", "#c-category4", function () {
                    $("#c-areaId").val( $("#c-category4").val());
                });
                $(document).on("change", "#c-bankAreaIds4", function () {
                    $("#c-bankAreaIds").val( $("#c-bankAreaIds1").val()+","+$("#c-bankAreaIds2").val()+","+$("#c-bankAreaIds3").val()+","+$("#c-bankAreaIds4").val());
                });
                //四级地区默认值设置（只执行一次）
                $(document).one("fa.event.businessAreaupdated", "#c-businessArea1", function () {
                    var a = $("#c-businessAreaPath").val().split(",");
                    $("#c-businessArea1").val(a[0]);
                    $("#c-businessArea2").val(a[1]);
                    $("#c-businessArea3").val(a[2]);
                    $("#c-businessArea4").val(a[3]);
                });
                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                    var a = $("#c-areaIds").val().split(",");
                    $("#c-category1").val(a[0]);
                    $("#c-category2").val(a[1]);
                    $("#c-category3").val(a[2]);
                    $("#c-category4").val(a[3]);
                });
                $(document).one("fa.event.bankAreaIdsupdated", "#c-bankAreaIds1", function () {
                    var a = $("#c-bankAreaIds").val().split(",");
                    $("#c-bankAreaIds1").val(a[0]);
                    $("#c-bankAreaIds2").val(a[1]);
                    $("#c-bankAreaIds3").val(a[2]);
                    $("#c-bankAreaIds4").val(a[3]);
                });
            }
        }
    };
    return Controller;
});